/**
 * Created by cjd on 2016/6/14.
 */
angular.module("app").controller("group.verify.Ctrl", ["$scope", "groupVerify","$stateParams", function ($scope, groupVerify,$stateParams) {
    $scope.statusEnum = {"0":"待审核","-1":"已拒绝","2":"已通过"};
    $scope.columns = [
        {
        dataField: "groupName",
        alignment: "center",
        caption: "申请社团"
    }, 
        {
        dataField: "createTime",
        alignment: "center",
        cellTemplate: "createTimeCellTemplate",
        caption: "申请日期"
    },  
        {
        dataField: "status",
        alignment: "center",
        cellTemplate: "statusCellTemplate",
        width: 60,
        caption: "状态"
    }, 
        null, 
        {
        dataField: "operation",
        alignment: "center",
        cellTemplate: "operationCellTemplate",
        caption: "操作"
    }
    ];
    $scope.orgVerifyListGridConfig = {
        bindingOptions: {
            selectedRowKeys: "selectedItems",
            dataSource: "orgVerifyListGridConfig.dataSource",
            columns: "columns"
        },
        twoWayBindingEnabled: false,
        searchPanel: {
            visible: true,
            width: 200,
            placeholder: "输入关键字进行查询"
        },
        paging: {
            pageSize: 15
        },
        groupPanel: {
            visible: true,
            emptyPanelText: "拖动列名进行分组和排序"
        },
        hoverStateEnabled: true,
        showBorders: true,
        showRowLines: true,
        rowAlternationEnabled: true,
        noDataText: "没有数据"
    };
    $scope.orgVerifyHistoryGridConfig = {
        bindingOptions: {
            selectedRowKeys: "selectedItems",
            dataSource: "orgVerifyHistoryGridConfig.dataSource"
        },
        twoWayBindingEnabled: false,
        // searchPanel: {
        //     visible: true,
        //     width: 200,
        //     placeholder: "输入关键字进行查询"
        // },
        paging: {
            pageSize: 15
        },
        groupPanel: {
            visible: true,
            emptyPanelText: "拖动列名进行分组和排序"
        },
        hoverStateEnabled: true,
        showBorders: true,
        showRowLines: true,
        rowAlternationEnabled: true,
        noDataText: "没有数据",
        columns: [{
            dataField: "createTime",
            alignment: "center",
            cellTemplate: "createTimeCellTemplate",
            caption: "申请日期"
        }, {
            dataField: "operation",
            alignment: "center",
            cellTemplate: "operationCellTemplate",
            caption: "审核"
        }]
    };
    $scope.changeStatus = function(status){
        if($scope.dataSource && $scope.dataSource.length){
            $scope.orgVerifyListGridConfig.dataSource = $scope.dataSource.filter(function(item){
                return item.status == status;
            });
            if(status == 0 && $scope.columns[3]){
                delete $scope.columns[3]
            }
            else if(status != 0 && !$scope.columns[3]){
                $scope.columns[3] = {
                    dataField: "verifyTime",
                    alignment: "center",
                    cellTemplate: "verifyTimeCellTemplate",
                    caption: "审核日期"
                }
            }
            $scope.orgVerifyListCount = $scope.orgVerifyListGridConfig.dataSource.length;
            for (var i in $scope.$parent.verifyTabs) {
                $scope.$parent.verifyTabs[i] = false;
            }
            $scope.$parent.verifyTabs[status] = true;
        }
    };
    $scope.showGroupInfo = function (applyInfo) {
        location.href = "#/home/group/info/" + applyInfo.groupId;
    };
    $scope.Verify = function(applyInfo){
        if(applyInfo.status == 0){
            $scope.applyInfo = angular.copy(applyInfo);
        }
        $scope.showVerifyHistory(applyInfo);
    };
    $scope.showVerifyHistory = function(applyInfo){
        $scope.type = "view";
        $scope.orgVerifyHistoryGridConfig.dataSource = $scope.dataSource.filter(function(item){
            return item.groupId == applyInfo.groupId;
        });
        $scope.orgVerifyHistoryGridConfig.dataSource.sort(function(a,b){return b.verifyTime - a.verifyTime});
    };
    $scope.getList = function(){
        groupVerify.getList({}, {test: 1}, function (data) {
            if (data.statusCode == 0) {
                $scope.dataSource = data.data;
                $scope.orgVerifyListGridConfig.dataSource = $scope.dataSource.filter(function(item){
                    return item.status == 0;
                });
                $scope.orgVerifyListCount = $scope.orgVerifyListGridConfig.dataSource.length;
            }
        });
    };
    $scope.check = function(){
        if(confirm("是否确定执行该操作?")){
            if($scope.applyInfo.status == -1){
                $scope.checkFail($scope.applyInfo);
            }
            else if($scope.applyInfo.status == 2){
                $scope.checkSuccess($scope.applyInfo);
            }
        }
    };
    $scope.checkSuccess = function(applyInfo){
        groupVerify.checkSuccess({}, {id: applyInfo.id,remark:applyInfo.remark}, function (data) {
            if (data.statusCode == 0) {
                alert("操作成功!");
                $scope.getList();
                $scope.applyInfo = {};
                $scope.type = "list";
            }
            else{
                alert(data.statusMessage)
            }
        });
    };
    $scope.checkFail = function(applyInfo){
        groupVerify.checkFail({}, {id: applyInfo.id,remark:applyInfo.remark}, function (data) {
            if (data.statusCode == 0) {
                alert("操作成功!");
                $scope.getList();
                $scope.applyInfo = {};
                $scope.type = "list";
            }
            else{
                alert(data.statusMessage)
            }
        });
    };
    $scope.getList();
}]);